﻿using D.B.Data.API.Applications.Command;
using D.B.Data.Domain;
using D.B.Data.ErrorCode;
using D.B.Data.Infrastructuse;
using MediatR;

namespace D.B.Data.API.Applications.CommandHandler
{
    public class DelUserCommandHandler : IRequestHandler<DelUserCommand, APIResult<object>>
    {
        private readonly IBaseRepository<UserModel> baseRepository;

        public DelUserCommandHandler(IBaseRepository<UserModel> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        public Task<APIResult<object>> Handle(DelUserCommand request, CancellationToken cancellationToken)
        {
            var res = baseRepository.GetAll().FirstOrDefault(s => s.Id == request.Id);
            res.IsDel = true;
           var a=baseRepository.Update(res);
            APIResult<object> aPIResult = new();
            if (a==1)
            {
                aPIResult.Code = EnumCode.Ok;
                aPIResult.Msg = "删除成功";
                aPIResult.Data = a;

            }
            else
            {
                aPIResult.Code = EnumCode.File;
                aPIResult.Msg = "删除失败";
                aPIResult.Data = a;
            }

            return Task.FromResult(aPIResult);
        }
    }
}
